/**
 * Created by JetBrains PhpStorm.
 * User: Ernest Dey
 * Date: 4/18/12
 * Time: 4:04 AM
 * To change this template use File | Settings | File Templates.
 */

var Custom = {

    init: function() {
        $("a.fncyBx").fancybox({
            'transitionIn'	: 'fade',
            'scrolling'		: 'no',
            'titleShow'		: false,
            'overlayOpacity'	: '20',
            'hideOnOverlayClick': false
            /*'onClosed'		: function() {
                $("#login_error").hide();
            }*/
        });

        this.bindEvent();
    },

    bindEvent: function() {

        $('#form_a .chSel_all').click(function() {
            $("#form_a input[type='checkbox']").each(function(k, v) {
                $(v).removeAttr('checked');
            });
            $('.objectiveComponent').fadeOut('fast');
            return false;
        });

        $("#form_a input[type='checkbox']").not('.chSel_all').change(function() {
            $("#form_a input[type='checkbox']").each(function(k, v) {
                $(v).removeAttr('checked');
            });
            $(this).attr('checked', 'checked');

            $("input[name='question_id']").val($(this).parent().parent().attr('data-id'));
            $('.objectiveComponent').fadeIn('fast');
            //console.log($(this).parent().parent());
        });

        $('.msg_box').click(function(e) {
            e.preventDefault();
            $(this).fadeOut("slow");
        });

        $('.delete_all_dt').click( function() {
            var id = $(this).parent().parent().attr('data-id');
            /*oTable = $('.data_table').dataTable();
            $('input[@name=row_sel]:checked', oTable.fnGetNodes()).closest('tr').fadeTo(600, 0, function () {
                oTable.fnDeleteRow( this );
                $('.chSel_all','.data_table').attr('checked',false);
                return false;
            });*/
        });

        $("input[name=update]").click(function(e) {
            e.preventDefault();
            $(this).val('Updating...');
            var data = $('#edit_form').serialize();
            Custom.sendData(data, '?isAjaxUpdate=1', Custom.notify);
        });

        $('a.edit').click(function(e) {
            var url = window.location.href;
            $('.msg_box').hide();
            e.preventDefault();
            var id = $(this).parent().parent().attr('data-id');
            var table = $(this).parent().parent().attr('data-table');
            var form = '#' + $(this).attr('data-target');
            var data = {
                url: url + '?isAjax=1',
                data: {id: id, table: table},
                type: 'post',
                dataType: 'json'
            };
            Custom.getData(data, form, Custom.populateFields);
        });

        $('.delete').click(function(e) {
            var id = $(this).parent().parent().attr('data-id');
            e.preventDefault();
            if (confirm('Are you sure?')) {
                var data = {
                    id: id
                };
                Custom.ajaxDelete(data, '?isAjaxDelete=1', Custom.notify, this);
            };

        });
    },

    notify: function(response) {
        console.log(response);
        $('.msg_box').addClass(response.type).prepend(response.msg);
        $('#edit_form input, #edit_form select, #edit_form textarea')
            .not("#edit_form input[name='update'], #edit_form input[name='reset']").val('');
        $("#edit_form input[name='update']").attr('disabled', 'disabled').val('Updated');
        $('.msg_box').fadeIn('slow');
        //console.log(response);
    },

    getData: function (data, formName, callbackFunction) {
        $.ajax({
            url: data.url,
            type: data.type,
            data: data.data,
            dataType: data.dataType,
            success: function(callback) {
                callbackFunction(callback, formName);
            },
            error: function(xhr, text) {

            }
        });
    },

    sendData: function (data, url, callbackFunction) {
        var pageUrl = window.location.href;
        $.ajax({
            url: pageUrl + url,
            type: 'post',
            data: data,
            dataType:'json',
            success: function(callback) {
                callbackFunction(callback);
            },
            error: function(xhr, text) {
                alert(text);
            }
        });
    },

    ajaxDelete: function(data, url, callbackFunction, caller) {
        var pageUrl = window.location.href;
        $.ajax({
            url: pageUrl + url,
            type: 'post',
            data: data,
            dataType:'json',
            success: function(callback) {
                callbackFunction(callback);
                $(caller).parent().parent().remove();
            },
            error: function(xhr, text) {
                    alert(text);
            }
        });
    },

    populateFields: function(obj, formName) {
        //console.log(obj.id);
        $('input[name=id]').val(obj.id);
        $.each(obj, function(k, v) {
            if (k == 'number') {
                $.each($("select[name='number'] option"), function(i, j) {
                    if ($(j).val() == v) {
                        $(j).attr('selected', 'selected');
                    }
                });
            } else if (k == 'level') {
                $.each($("#level option"), function(i, j) {
                    if ($(j).val() == v) {
                        $(j).attr('selected', 'selected');
                    }
                });
            } else if (k == 'objective') {
                $(formName + " textarea[name='" + k + "']").html(v);
            } else if (k == 'objective_index') {
                $.each($("select[name='objective_index'] option"), function(i, j) {
                    if ($(j).val() == v) {
                        $(j).attr('selected', 'selected');
                    }
                });
            } else if (k == 'question_id') {
                $.each($("select[name='question_id']:last option"), function(i, j) {
                    if ($(j).val() == v) {
                        $(j).attr('selected', 'selected');
                    }
                });
            } else if (k == 'recomendation_group_id') {
                $.each($("select[name='recomendation_group_id']:last option"), function(i, j) {
                    if ($(j).val() == v) {
                        $(j).attr('selected', 'selected');
                    }
                });
            } else if (k == 'major_category_id') {
                $.each($("select[name='major_category_id']:last option"), function(i, j) {
                    if ($(j).val() == v) {
                        $(j).attr('selected', 'selected');
                    }
                });
            } else if (k == 'school_type') {
                $.each($("select[name='school_type'] option"), function(i, j) {
                    if ($(j).val() == v) {
                        $(j).attr('selected', 'selected');
                    }
                });
            }
            $(formName + " input[name='" + k + "']").val(v);
        });
    },

    showObjectiveComponent: function() {

    }
};

$(function() {
    Custom.init();
})